﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AppWPF.Models;
using AppWPF.ViewModels;
using System.ComponentModel;

namespace AppWPF.Views.Pacientes
{
    public partial class MainWindow : Window
    {
        private HospitalEntities contexto = new HospitalEntities();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnMod_Click(object sender, RoutedEventArgs e)
        {
            ModificarPaciente mp = new ModificarPaciente();
            if (listPacientes.SelectedItem == null)
            {
                MessageBox.Show("Seleccione un paciente de la lista", "Atencion", MessageBoxButton.OK);
            }
            else
            {
                Paciente paciente = (Paciente)listPacientes.SelectedItem;
                mp.LayoutMPac.DataContext = paciente;
                mp.ShowDialog();
            }
        }

        private bool FilterPaciente(object obj)
        {
            if (FindRut.Text.Length == 0) return true;

            Paciente item = obj as Paciente;
            if (item == null) return false;

            double Find = double.Parse(FindRut.Text);
            if (Find == 0) return true;

            if (item.Rut.ToString().Contains(Find.ToString())) return true;

            return false;
        }

        private void FindRut_TextChanged(object sender, TextChangedEventArgs e)
        {
            ICollectionView view = CollectionViewSource.GetDefaultView(listPacientes.ItemsSource);
            view.Filter = new Predicate<object>(FilterPaciente);

            view.SortDescriptions.Clear();
            view.SortDescriptions.Add(new SortDescription("Rut", ListSortDirection.Ascending));
        }

        private void FindRut_KeyDown(object sender, KeyEventArgs e)
        {
            List<string> KeyPads = new List<string>() { "D0","D1","D2","D3","D4","D5","D6","D7","D8","D9" };
            if (KeyPads.Contains(e.Key.ToString()))
            {
                e.Handled = false;
            }
            else
            {
                FindRut.Clear();
                e.Handled = true;
            }
        }

        //private void FindRut_TextKeyChanged(object sender, KeyEventArgs e)
        //{
        //    
        //}
    }
}
